package com.gy.studyonlineapp.activity;

import static android.content.ContentValues.TAG;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.gy.studyonlineapp.R;
import com.gy.studyonlineapp.callback.RegAVQueryObserver;
import com.gy.studyonlineapp.common.Constants;
import com.gy.studyonlineapp.constants.SharedPreference;
import com.gy.studyonlineapp.entity.User;

import java.io.IOException;

import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_register);
        EditText userName = this.findViewById(R.id.UserNameEdit);
        EditText passWord = this.findViewById(R.id.PassWordEdit);
        EditText passWordAgain = this.findViewById(R.id.PassWordAgainEdit);
        Button signUpButton = this.findViewById(R.id.SignUpButton);
        Button backLoginButton = this.findViewById(R.id.BackLoginButton);

        //立即注册按钮监听器
        signUpButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {//判断输入是否为空，判断输入密码是否一致
                        if(userName.getText() != null && !userName.getText().toString().isEmpty()
                                && passWord.getText() != null && !passWord.getText().toString().isEmpty()
                                && passWordAgain.getText()!= null && !passWordAgain.getText().toString().isEmpty()){
                            if(passWord.getText().toString().equals(passWordAgain.getText().toString())){
//                                AVQuery<AVObject> query = new AVQuery<>("StudyOnlineUser");
//                                query.whereEqualTo("username",userName.getText().toString());
//                                query.findInBackground().subscribe(new RegAVQueryObserver(RegisterActivity.this,
//                                        userName.getText().toString(), passWord.getText().toString()));
                                String url = "http://" + Constants.localhost + ":8080/search/user";
                                OkHttpClient client = new OkHttpClient();
                                RequestBody requestBody = new FormBody.Builder()
                                        .add("username",userName.getText().toString())
                                        .build();
                                Request request = new Request.Builder()
                                        .url(url)
                                        .post(requestBody)
                                        .build();
                                Call call = client.newCall(request);
                                call.enqueue(new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {}
                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {
                                        String user = response.body().string();
                                        if (user.isEmpty()) {
                                            String url = "http://" + Constants.localhost + ":8080/reg";
                                            OkHttpClient client = new OkHttpClient();
                                            RequestBody requestBody = new FormBody.Builder()
                                                    .add("username",userName.getText().toString())
                                                    .add("password",passWord.getText().toString())
                                                    .build();
                                            Request request = new Request.Builder()
                                                    .url(url)
                                                    .post(requestBody)
                                                    .build();
                                            Call call2 = client.newCall(request);
                                            call2.enqueue(new Callback() {
                                                @Override
                                                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                                }

                                                @Override
                                                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                                    //注册成功并保存到数据库，跳转至登录页面
                                                    Looper.prepare();
                                                    Toast.makeText(RegisterActivity.this,"注册成功!",Toast.LENGTH_LONG).show();
                                                    RegisterActivity.this.startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                                                    Looper.loop();
                                                }
                                            });
                                        } else {
                                            Looper.prepare();
                                            Toast.makeText(RegisterActivity.this,"该用户名已被注册，请重新输入用户名",Toast.LENGTH_LONG).show();
                                            Looper.loop();
                                        }
                                    }
                                });
                            }else {
                                Toast.makeText(RegisterActivity.this,"两次密码输入不一致",Toast.LENGTH_LONG).show();
                            }
                        }else {
                            Toast.makeText(RegisterActivity.this,"输入新的账号密码不能为空",Toast.LENGTH_LONG).show();
                        }
                    }
                }
        );
        //返回登录按钮监听器
        backLoginButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }
}
